//test.cpp
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
#include<time.h>

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<random>
using namespace std;

class CreateInput{
public:

    CreateInput(){
        for(char c = 'a'; c <= 'z'; c++){
            chars.push_back(c);
        }
        for(char c = 'A'; c <= 'A'; c++){
            chars.push_back(c);
        }
    }


    ~CreateInput(){
        while (!stdin_format.empty()) {
            stdin_format.erase(stdin_format.begin());
        }
        while (!chars.empty()) {
            chars.erase(chars.begin());
        }
        while (!input_file.empty()) {
            input_file.erase(input_file.begin());
        }
    }

    void travel_dir(const char* path){
        DIR *d;
        struct dirent* file = new dirent();
        struct stat* st = new struct stat();
        if(!(d = opendir(path))){ // 打不开指定的起始文件夹
            printf("erroe dirs %s!!!\n", path);
            return;
        }

        while((file = readdir(d)) != NULL){
            // 跳过隐藏文件, 当前目录(.),或上一级目录
            if(strncmp(file->d_name, ".", 1) == 0)
                continue;
            //如果是普通的文件
            if(file->d_type == 8){
                char* file_path = (char *)malloc(strlen(path) + strlen(file->d_name) + 2);
                char* input_file_path = (char *)malloc(strlen(path) + strlen(file->d_name) + 2);

                memset(file_path, '\0', sizeof(file_path));
                memset(input_file_path, '\0', sizeof(input_file_path));

                strcat(file_path, path);
                strcat(input_file_path, path);

                strcat(file_path, "/");
                strcat(input_file_path, "/input.txt");

                strcat(file_path, file->d_name);

                if(strncmp(file->d_name, "stdin_format.txt", 16) == 0){
                    this->stdin_format.push_back(file_path);
                    this->input_file.push_back(input_file_path);
                }
            }
            //如果是文件夹
            if(file->d_type == 4){
                char* dir_path = (char *)malloc(strlen(path) + strlen(file->d_name) + 2);
                memset(dir_path, '\0', sizeof(dir_path));
                strcat(dir_path, path);
                strcat(dir_path, "/");
                strcat(dir_path, file->d_name);
                travel_dir(dir_path);
            }
        }
        closedir(d);
        return;
    }

    void generateInput() {
        string info = "\0";
        string number = "";
        ifstream ifs_stdin_format;
        ofstream ofs_input;
	    srand((unsigned)time(NULL));

        for(int index = 0; index < stdin_format.size(); index++){

            ifs_stdin_format.open(stdin_format[index], ios::in);
            if (!ifs_stdin_format.is_open()){
                std::cout << "++++stdin_format.txt open fail++++" << endl;
                continue;
            }

            ofs_input.open(input_file[index], ios::out | ios::trunc);
            if (!ofs_input.is_open()){
                cout << "++++input.txt open fail++++" << endl;
                continue;
            }


            while(getline(ifs_stdin_format, info, ' ')){
                if(info.length() >= 3 && strncmp(info.substr(0,3).c_str(), "int", 3) == 0){
                    int start = 0, end = 0;

                    number = "";
                    int i = 4;
                    for(; info[i] != ','; i++)
                        number += info[i];
                    start = std::atoi(number.c_str());
                    i++;

                    number = "";
                    for(; info[i] != ')'; i++)
                        number += info[i];
                    end = std::atoi(number.c_str());
                    ofs_input << (rand()%(end-start+1))+start << ' ';
                }
                else if(info.length() >= 4 && strncmp(info.substr(0,4).c_str(), "char", 4) == 0){
                    ofs_input << chars[rand()%(chars.size())]<< ' ';
                }
                else if(info.length() >= 6 && strncmp(info.substr(0,6).c_str(), "string", 6) == 0){
                    int start = 0, end = 0;

                    number = "";
                    int i = 7;
                    for(; info[i] != ','; i++)
                        number += info[i];
                    start = std::atoi(number.c_str());
                    i++;

                    number = "";
                    for(; info[i] != ')'; i++)
                        number += info[i];
                    end = std::atoi(number.c_str());

                    int lengh = (rand()%(end-start+1))+start;
                    for(int j = 0; j < lengh; j++)
                        ofs_input << chars[rand()%(chars.size())];
                    ofs_input << ' ';
                }
            }
        
            ifs_stdin_format.close();
            ofs_input.close();
        }
    }

private:
    vector<char> chars;
    vector<string> stdin_format;
    vector<string> input_file;
};



// int main(int argc, char * argv[]){
//     CreateInput *create_input = new CreateInput();
//     create_input->travel_dir("./input");
//     create_input->generateInput();
//     delete create_input;
//     return 0;
// }

